<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/12/21
  Time: 10:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${ctx}/css/bootstrap.min.css">
    <script src="${ctx}/js/jquery.min.js"></script>
    <script src="${ctx}/js/bootstrap.min.js"></script>
    <script src="${ctx}/My97DatePicker/WdatePicker.js"></script>
    <script>
        $(function () {
            $.ajax({
                url: "${ctx}/load.do",
                type: "post",
                dataType: "json",
                success: function(data){
                    $.each(data,function(index,value){
                        var  $option=$("<option value='"+value.cid+"'>"+value.cname+"</option>");
                        $('select').append($option);
                    })
                },
                error: function () {
                    alert("上传失败!")
                }
            })
        });


        function uploadPicture() {
            var formData = new FormData();
            formData.append("pic", document.querySelector("#cover").files[0]);
            formData.append("enctype", "multipart/form-data");
            $.ajax({
                url: "${ctx}/upload.do",
                type: "post",
                data: formData,
                dataType: "text",
                contentType: false,
                processData: false,
                success: function (data) {
                    $("#uploadPic").attr("src", "${ctx}/" + data);
                    $('#pic_src').val(data);
                },
                error: function () {
                    alert("上传失败！");
                }
            })
        }
    </script>
</head>
<body>
<h2>图书添加</h2>
<hr/>
<form action="${ctx}/insert.do" method="post">
    <table class="table">
        <tr>
            <td>图书名</td>
            <td><input type="text" name="bookName"/></td>
        </tr>
        <tr>
            <input type="hidden" name="cover" id="pic_src"/>
            <td>
                封面图片<input type="button" onclick="uploadPicture()" value="上传"/>
            </td>
            <td><input type="file" id="cover"/></td>
        </tr>
        <tr>
            <td><img src="" id="uploadPic"/></td>
        </tr>
        <tr>
            <td>价格</td>
            <td><input type="text" name="price"/></td>
        </tr>
        <tr>
            <td>作者</td>
            <td><input type="text" name="author"/></td>
        </tr>
        <tr>
            <td>出版社</td>
            <td><input type="text" name="press"/></td>
        </tr>
        <tr>
            <td>出版日期</td>
            <td><input type="text" name="pubDate" onclick="WdatePicker({readOnly:true})"/>
            </td>
        </tr>
        <tr>
            <td>图书分类</td>
            <td>
                <select name="category_id">

                </select>
            </td>
        </tr>
        <tr>
            <td><input type="submit" value="添加"></td>

        </tr>
    </table>
</form>
</body>
</html>
